
const db = require('../../MySql');
/**
     * 构造函数 - 创建考试记录考题对象实例
     * 用于封装考试答卷中每道题目的答题状态、得分和相关信息
     * 对应数据库中的 el_paper_qu 表，记录考生对每道题目的作答情况
     * 
 */
class el_paper_qu {
    /**
     * @param {string} id - 记录ID，唯一标识符
     * @param {string} paper_id - 试卷ID，关联的考试答卷
     * @param {string} qu_id - 题目ID，关联的试题
     * @param {number} qu_type - 题目类型（1-单选题，2-多选题，3-判断题，4-简答题等）
     * @param {boolean} answered - 是否已答，true表示考生已经作答
     * @param {string} answer - 主观答案，简答题等主观题的答案内容
     * @param {number} sort - 题目排序，题目在试卷中的显示顺序
     * @param {number} score - 题目分值，该题目的满分
     * @param {number} actual_score - 实际得分，考生在该题目上获得的分数
     * @param {boolean} is_right - 是否答对，客观题自动判定的正确性
     * 
     */
    constructor(id, paper_id, qu_id, qu_type, answered, answer, sort, score, actual_score, is_right, is_user_delete) {
        this.id = id
        this.paperId = paper_id
        this.quId = qu_id
        this.quType = qu_type
        this.answered = answered
        this.answer = answer
        this.sort = sort
        this.score = score
        this.actualScore = actual_score
        this.isRight = is_right
        this.is_user_delete = is_user_delete
    }


    /**
     * 根据paper_id查询el_paper_qu关联的题目
     * @param {string} paper_id 试卷ID，关联的考试答卷
     * @returns 考试记录考题对象实例数组
     */
    static async findPaperQuByPaperId(paper_id) {
        const [results] = await db.query(`
            SELECT * FROM el_paper_qu WHERE paper_id=?
        `, [paper_id])
        return results
    }

    /**
     * 根据qu_id查询el_paper_qu关联的题目
     * @param {string} qu_id 题目ID，关联的试题
     * @returns 考试记录考题对象实例数组
     */
    static async findPaperQuByQuId(qu_id) {
        const [results] = await db.query(`
            SELECT * FROM el_paper_qu WHERE qu_id=?
        `, [qu_id])
        return results
    }

    /**
     * 根据试卷ID和题目ID查询考试记录考题对象实例，用于获取考生在该试卷上的答题情况
     * @param {string} paper_id - 试卷ID，关联的考试答卷
     * @param {string} qu_id - 题目ID，关联的试题
     * @returns {Promise<object>} - 数据库操作结果对象
     */
    static async findPaperQuByPaperIdAndQuId(paper_id, qu_id) {
        const [results] = await db.query(`
            SELECT * FROM el_paper_qu WHERE paper_id=? AND qu_id=?
        `, [paper_id, qu_id])
        return results[0]
    }

    static async findPaperQuByPaperIdAndIsUserDelete(paper_id, is_user_delete) {
        const [results] = await db.query(`
            SELECT * FROM el_paper_qu WHERE paper_id=? and is_user_delete =?
        `, [paper_id, is_user_delete])
        return results
    }


    /**
     * 新增考试记录考题
     * @param {el_paper_qu} paperQu 考试记录考题对象实例
     * @returns 数据库操作结果对象
     */
    static async insertPaperQu(paperQu) {
        const { id, paper_id, qu_id, qu_type, answered, answer, sort, score, actual_score, is_right } = paperQu
        const [results] = await db.query(`
            INSERT INTO el_paper_qu (id, paper_id, qu_id, qu_type, answered, answer, sort, score, actual_score, is_right)
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
        `, [id, paper_id, qu_id, qu_type, answered, answer, sort, score, actual_score, is_right])


        console.log('新增考试记录考题:', results)
        return results
    }


    /**
     * @param {string} paper_id - 试卷ID，关联的考试答卷
     * @param {string} qu_id - 题目ID，关联的试题
     * @param {boolean} answered - 是否已答，true表示考生已经作答
     * @returns {Promise<object>} - 数据库操作结果对象
     * 功能描述：
     * 更新考试记录考题对象实例的answered字段，用于标记考生是否已作答该题目
     */
    static async updateAnswered(paper_id, qu_id, answered) {
        const [results] = await db.query(`
            update el_paper_qu set answered=? where paper_id=? and qu_id=?
        `, [paper_id, qu_id, answered])
        return results
    }

    /**
     * @param {string} paper_id - 试卷ID，关联的考试答卷
     * @param {string} qu_id - 题目ID，关联的试题
     * @param {boolean} is_right - 是否答对，true表示考生已答对该题目
     * @returns {Promise<object>} - 数据库操作结果对象
     */
    static async updateIsRight(paper_id, qu_id, is_right) {
        const [results] = await db.query(`
            update el_paper_qu set is_right=? where paper_id=? and qu_id=?
        `, [is_right, paper_id, qu_id])
        return results
    }

    /**
     * 
     */
    static async updateIsUserDelete(paperId, quId, is_user_delete) {
        const [results] = await db.query(`
            update el_paper_qu set is_user_delete=? where paper_id=? and qu_id=?
        `, [is_user_delete, paperId, quId])
        return results
    }

}

module.exports = el_paper_qu